package com.borrowed.book.system.controller;

import com.borrowed.book.system.domain.*;
import com.borrowed.book.system.exception.ReDictKeyException;
import com.borrowed.book.system.exception.RecodeDictException;
import com.borrowed.book.system.service.ISysDictService;
import com.borrowed.book.system.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "字典管理")
@RequestMapping("/api")
public class SysDictController extends BaseController{
    @Autowired
    private ISysDictService dictService;


    @ApiOperation(value = "获取数据字典类型",  httpMethod = "GET")
    @GetMapping("/getDictType")
    public TableDataInfo getDictType(SelectDictTypeInDto param){
        startPage();
        List<SysDictType> list=dictService.selectDictTypeList(param);
        return getDataTable(list);
    }


    @ApiOperation(value = "添加数据字典类型",  httpMethod = "POST")
    @PostMapping("/addDictType")
    public AjaxResult addDictType(@RequestBody SysDictType param){
        AjaxResult result;
        try{
            dictService.addDictType(param);
            result=AjaxResult.sucess();
        }catch (RecodeDictException e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除数据字典类型",  httpMethod = "DELETE")
    @ApiImplicitParam(name="code",value = "code",type = "String",required = true)
    @DeleteMapping("/deleteDictType")
    public AjaxResult deleteDictType(String code){
        AjaxResult result;
        try{
            dictService.deleteDictTypeByCode(code);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "根据字典类型获取属性",  httpMethod = "GET")
    @ApiImplicitParam(name="code",value = "数据字典类型code")
    @GetMapping("/getDict")
    public TableDataInfo getDictByCode(String code){
        startPage();
        List<SysDict> list=dictService.selectDictByType(code);
        return getDataTable(list);
    }


    @ApiOperation(value = "添加数据字典属性",  httpMethod = "POST")
    @PostMapping("/addDict")
    public AjaxResult addDict(@RequestBody SysDict param){
        AjaxResult result;
        try{
            dictService.addDict(param);
            result=AjaxResult.sucess();
        }catch (ReDictKeyException e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "删除数据字典属性",  httpMethod = "DELETE")
    @ApiImplicitParam(name="id",value = "1",type = "Long",required = true)
    @DeleteMapping("/deleteDict")
    public AjaxResult deleteDict(Long id){
        AjaxResult result;
        try{
            dictService.deleteDictById(id);
            result=AjaxResult.sucess();
        }catch (Exception e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "修改数据字典属性",  httpMethod = "PUT")
    @PutMapping("/updateDict")
    public AjaxResult updateDict(@RequestBody SysDict param){
        AjaxResult result;
        try{
            dictService.updateDict(param);
            result=AjaxResult.sucess();
        }catch (ReDictKeyException e){
            e.printStackTrace();
            result=AjaxResult.error(e.getMessage());
        }
        return result;
    }
}
